package org.misty.redis.autoconfigure.common;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.misty.redis.bloomfilter.IFilterConfig;
import org.misty.redis.locker.ILockerConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.HashMap;
import java.util.Map;

import static org.misty.redis.autoconfigure.common.Constants.CONFIGURATION_ROOT;


/**
 * @author Misty on 2020-06-11
 */
@Getter
@Setter
@ConfigurationProperties(prefix = CONFIGURATION_ROOT)
public class MultiConfigProperties {
    private static volatile MultiConfigProperties INSTANCE;

    private Map<String, FilterConfig> filter = new HashMap<>();

    private Map<String, LockerConfig> locker = new HashMap<>();

    private Map<String, CacheConfig> cache = new HashMap<>();

    @Getter
    @Setter
    @ToString
    public static class FilterConfig extends AdditionalConfig implements IFilterConfig {
        /**
         * 期望元素个数
         */
        private Long expectedInsertions;
        /**
         * 假阳性率
         */
        private Double fpp;
        /**
         * redis中所使用的key
         * 不能重复，否则报错
         */
        private String redisKey;

        /**
         * RedisClient bean 实例名称
         * 通过beanFactory.getBean(clientBean)获取
         * 如果未设置，则通过beanFactory.getBean(RedisClient.class)获取
         */
        private String clientBean;
    }

    @Getter
    @Setter
    @ToString
    public static class LockerConfig extends AdditionalConfig implements ILockerConfig {
        /**
         * 锁超时时间 (ms)
         * 默认 10s
         */
        private Integer expireTime;

        /**
         * RedisClient bean 实例名称
         * 通过beanFactory.getBean(clientBean)获取
         * 如果未设置，则通过beanFactory.getBean(RedisClient.class)获取
         */
        private Integer acquireTimeout;

        /**
         * RedisClient bean 实例名称
         * 通过beanFactory.getBean(clientBean)获取
         * 如果未设置，则通过beanFactory.getBean(RedisClient.class)获取
         */
        private String clientBean;
    }

    @Getter
    @Setter
    @ToString
    public static class CacheConfig extends AdditionalConfig {
        /**
         * redis中所使用的lock key前缀
         * 不能重复，否则报错
         * 默认值：`cache:{name}:`
         */
        private String lockKeyPrefix;

        /**
         * RedisLocker bean 实例名称
         * 通过beanFactory.getBean(lockerBean)获取
         * 如果未设置，则通过beanFactory.getBean(RedisLocker.class)获取
         */
        private String lockerBean;

        /**
         * 可选
         * BloomFilter bean 实例名称
         * 通过beanFactory.getBean(filterBean)获取
         * 如果未设置，则不使用布隆过滤器
         */
        private String filterBean;
    }

}
